package edu.tongji.jiaowu.webapp.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.tongji.quickCRUDQ.model.CommonBean;
import com.tongji.quickCRUDQ.util.JsonDataSet;
import com.tongji.quickCRUDQ.util.RequestUtil;
import com.tongji.quickCRUDQ.util.StringUtil;

import edu.tongji.jiaowu.service.EvItemManager;
import edu.tongji.jiaowu.service.EvQuestionVManager;
import edu.tongji.jiaowu.service.EvStandardManager;
import edu.tongji.jiaowu.service.EvtStudentManager;
import edu.tongji.jiaowu.service.EvtStudentResultManager;
import edu.tongji.jiaowu.service.ParameterManager;
import edu.tongji.jiaowu.service.VcodeManager;
import edu.tongji.jiaowu.webapp.session.SessionParameter;
import edu.tongji.jiaowu.webapp.session.SessionParameterLocal;

public class EvaluationController extends MultiActionController{
	private EvtStudentManager evtStudentManager;
	private EvtStudentResultManager evtStudentResultManager;
	private EvQuestionVManager evQuestionVManager;
	private EvItemManager evItemManager;
	private EvStandardManager evStandardManager;
	private ParameterManager parameterManager;
	private VcodeManager vcodeManager;
	 
	public ModelAndView evtStudentController(HttpServletRequest request,HttpServletResponse response){
		String cmd=request.getParameter("command");
		SessionParameter sp=SessionParameterLocal.getSessionParameter();
		String year=sp.getCurrentSchoolYear();
		String semester=sp.getCurrentSchoolSemester();
		if ("evt_student_init".equals(cmd)) {//学生评教初始化
			evtStudentManager.init(year,semester);
		} else if ("evt_student_switch".equals(cmd)){//学生评教开关
			String evtSwitch=request.getParameter("evtSwitch");
			Map parameter=new HashMap();
			parameter.put("EVT_STUDENT_SWITCH", evtSwitch);
			parameterManager.updateParameter(parameter);
		}else if ("evt_student_deal".equals(cmd)){//学生评教结果处理
			evtStudentManager.statistic(year,semester);
		}
		String evtStudentSwitch=parameterManager.findParameterValue("EVT_STUDENT_SWITCH");
		Map model=new HashMap();
	   model.put("evtStudentSwitch", evtStudentSwitch);
       return new ModelAndView("/evaluation/evt/student/controller","model",model);
	}
	
	
	public ModelAndView evtTeacherResult(HttpServletRequest request,HttpServletResponse response){
		SessionParameter sp=SessionParameterLocal.getSessionParameter();
		String tno=sp.getUserAccount();
		String year=sp.getCurrentSchoolYear();
		String semester=sp.getCurrentSchoolSemester();
		
        //1或null:教师查询教师统计结果;2:教务处查询教学班级(课程序号)统计结果;3:教务处查询院系统计结果;4:教务处查询教师统计结果
		String flag=StringUtil.NVL(request.getParameter("flag"),"1");
		CommonBean schoolYears=vcodeManager.findCodes("SCHOOL_YEAR");
		CommonBean schoolSemesters=vcodeManager.findCodes("SCHOOL_SEMESTER");
		String yearJson=JsonDataSet.commonbeanToJsonArray(schoolYears, "code", "name");
		String semesterJson=JsonDataSet.commonbeanToJsonArray(schoolSemesters, "code", "name");
		request.setAttribute("yearJson", yearJson);
		request.setAttribute("semesterJson", semesterJson);
		
		String url="forward:/quick/quick.html?tableName=T_EVT_STATISTIC&SCHOOL_YEAR="+year+"&SCHOOL_SEMESTER="+semester;
		if ("1".equals(flag)) {
			url+="&TEACHER_NO="+tno;
		}else if("3".equals(flag)){
			url+="&tableAlias=T_EVT_STATISTIC_DEPARTMENT";
		}else if("4".equals(flag)){
			url+="&tableAlias=T_EVT_STATISTIC_TEACHER";
		}
		return new ModelAndView(url);
	}
	
	
	
	public ModelAndView evtStudentComment(HttpServletRequest request,HttpServletResponse response){
		SessionParameter sp=SessionParameterLocal.getSessionParameter();
		String tno=sp.getUserAccount();
		String year=sp.getCurrentSchoolYear();
		String semester=sp.getCurrentSchoolSemester();
		
		String flag=StringUtil.NVL(request.getParameter("flag"),"1");//1或null:教师查询;2:教务处查询
		CommonBean schoolYears=vcodeManager.findCodes("SCHOOL_YEAR");
		CommonBean schoolSemesters=vcodeManager.findCodes("SCHOOL_SEMESTER");
		String yearJson=JsonDataSet.commonbeanToJsonArray(schoolYears, "code", "name");
		String semesterJson=JsonDataSet.commonbeanToJsonArray(schoolSemesters, "code", "name");
		request.setAttribute("yearJson", yearJson);
		request.setAttribute("semesterJson", semesterJson);
		
		String url="forward:/quick/quick.html?tableName=T_EVT_STUDENT&IS_FINISH=Y&SCHOOL_YEAR="+year+"&SCHOOL_SEMESTER="+semester;
		if ("1".equals(flag)) {
			url+="&tableAlias=T_EVT_STUDENT_COMMENT&TEACHER_NO="+tno;
		}else if ("2".equals(flag)) {
			url+="&tableAlias=T_EVT_STUDENT_COMMENT_ADMIN";
		}
		return new ModelAndView(url);
	}
	public ModelAndView evtStudent(HttpServletRequest request,HttpServletResponse response){
	    Map model=new HashMap();
		String evtStudentSwitch=parameterManager.findParameterValue("EVT_STUDENT_SWITCH");
		if ("N".equals(evtStudentSwitch)) {
			model.put("message", "学生评教未开放！");
			return new ModelAndView("/public/info","model",model);
		}
		
		SessionParameter sp=SessionParameterLocal.getSessionParameter();
		String sno=sp.getUserAccount();
		String year=sp.getCurrentSchoolYear();
		String semester=sp.getCurrentSchoolSemester();
	    String evtJson=evtStudentManager.findEvTeachersJson(year,semester,sno);
	    CommonBean standardBean=evStandardManager.findAllEvStandards();
	    String standardJson=JsonDataSet.commonbeanToJsonArray(standardBean,new String[]{"levelNo","levelName"});
	    
	    String classCode="EV_XSPJ";//学生评教
	    String courseType="01";//理论课
	    CommonBean questionBean=evQuestionVManager.findVEvQuestionByClass(year,semester,classCode, courseType);
	    String questionJson=JsonDataSet.commonbeanToJsonArray(questionBean, new String[]{"itemName","code","name"});
	    CommonBean itemBean=evItemManager.findEvItemByClass(year,semester,classCode, courseType);
	    String itemJson=JsonDataSet.commonbeanToJsonArray(itemBean, new String[]{"code","name"});
	    model.put("evtJson", evtJson);
	    model.put("standardJson", standardJson);
	    model.put("questionJson", questionJson);
	    model.put("itemJson", itemJson);
		return new ModelAndView("/evaluation/evt/student/evtStudent","model",model);
	}
	
	public ModelAndView studentEvTeaching(HttpServletRequest request,HttpServletResponse response){
		Map map=RequestUtil.mapParameter(request);
		String refid=(String) map.get("refid");
		String note=(String) map.get("NOTE");
		map.remove("refid");
		map.remove("NOTE");
		map.remove("ajax_extraParams");//去除自定义的ajax参数标记，参加ext-quick.js文件
		try {
			evtStudentResultManager.studentEvTeaching(refid,note,map);
			RequestUtil.writeJsonSuccess(response, "评教成功");
		} catch (Exception e) {
			RequestUtil.writeJsonException(response, e);
		}
		return null;
	}
	

	public EvtStudentManager getEvtStudentManager() {
		return evtStudentManager;
	}

	public void setEvtStudentManager(EvtStudentManager evtStudentManager) {
		this.evtStudentManager = evtStudentManager;
	}

	public EvtStudentResultManager getEvtStudentResultManager() {
		return evtStudentResultManager;
	}

	public void setEvtStudentResultManager(EvtStudentResultManager evtStudentResultManager) {
		this.evtStudentResultManager = evtStudentResultManager;
	}
	public ParameterManager getParameterManager() {
		return parameterManager;
	}
	public void setParameterManager(ParameterManager parameterManager) {
		this.parameterManager = parameterManager;
	}

	public EvQuestionVManager getEvQuestionVManager() {
		return evQuestionVManager;
	}

	public void setEvQuestionVManager(EvQuestionVManager evQuestionVManager) {
		this.evQuestionVManager = evQuestionVManager;
	}

	public EvItemManager getEvItemManager() {
		return evItemManager;
	}

	public void setEvItemManager(EvItemManager evItemManager) {
		this.evItemManager = evItemManager;
	}

	public EvStandardManager getEvStandardManager() {
		return evStandardManager;
	}

	public void setEvStandardManager(EvStandardManager evStandardManager) {
		this.evStandardManager = evStandardManager;
	}

	public VcodeManager getVcodeManager() {
		return vcodeManager;
	}

	public void setVcodeManager(VcodeManager vcodeManager) {
		this.vcodeManager = vcodeManager;
	}



}
